<?php
/**
 * utility Class for database
 */
class BDD {

    private $bdd;

    function __construct() {
        $this->bdd = new PDO("sqlite:bdd/bdd.sqlite3") or die;
    }

    /***********************************/
    /** fonction sur le porte_monnaie **/
    /***********************************/

    // ajouter une valeur (piece et billet) pour la devise EUR
    function addMoney($money)
    {
        error_log($money->getCurrency());
        $currencyId = $this->getIDCurrencyFromName($money->getCurrency());
        $valueNote = $money->getValueNote();
        $valuePiece = $money->getValuePiece();

        $insert = "insert into porte_monnaie(valueNote, valueMoney, devise_id) values(". $valueNote .", ". $valuePiece . ", ". $currencyId .")";
        
        error_log($insert);
        $statment = $this->bdd->prepare($insert);
        $statment->execute();
    }

    // total des pieces et billet
    function getMoneyFromCurrencyId($currencyId)
    {
        $resultQuery = $this->bdd->query("SELECT * FROM porte_monnaie WHERE devise_id=".$currencyId);
        $valueNote = 0;
        $valuePiece = 0;

        foreach($resultQuery as $row) {
            $valueNote += $row['valueNote'];
            $valuePiece += $row['valueMoney'];
        }

        $resultQuery = $this->bdd->query("SELECT * FROM devise WHERE id=".$currencyId);
        $name = "";

        foreach($resultQuery as $row) {

            $name = $row['name'];
        }

        return MoneyFactory::getDefaultFactory()->createMoney($valueNote, $valuePiece, $name);
    }

    function getTotalMoney()
    {
        $resultQuery = $this->bdd->query("SELECT * FROM porte_monnaie");
        $valueNote = 0;
        $valuePiece = 0;

        foreach($resultQuery as $row) {
            $valueNote += $row['valueNote'];
            $valuePiece += $row['valueMoney'];
        }

        return MoneyFactory::getDefaultFactory()->createMoney($valueNote, $valuePiece, "EUR");
    }

    /************/
    /* currency */
    /************/

    function getIDCurrencyFromName($currency){
        $resultQuery = $this->bdd->query("SELECt id FROM devise WHERE name='".$currency."'");
        $result = 0;

        foreach($resultQuery as $row) {
            $result = $row['id'];
        }
        return $result;
    }

    function addCurrency($currencyName){
        $insert = "";
        $insert = "insert into devise(name) values('".$currencyName."')";

        $statment = $this->bdd->prepare($insert);
        $statment->execute();
    }

    function selectAllCurrency(){
        $resultQuery = $this->bdd->query("SELECt id, name FROM devise");
        $result = array();

        foreach($resultQuery as $row) {
            $result = $result." / ".$row['id']." : ".$row['name'];
        }
        return $result;
    }

    /*********/
    /* other */
    /*********/

    function deleteAll()
    {
        $deleteUsers = "delete from user";
        $deletePorteMonnaie = "delete from porte_monnaie";
        $deleteDevise = "delete from devise";
        $deleteAutoIncrement = "delete from sqlite_sequence where name='devise'";

        $statment = $this->bdd->prepare($deleteUsers);
        $statment->execute();

        $statment = $this->bdd->prepare($deletePorteMonnaie);
        $statment->execute();

        $statment = $this->bdd->prepare($deleteDevise);
        $statment->execute();

        $statment = $this->bdd->prepare($deleteAutoIncrement);
        $statment->execute();
    }
}
?>
